<?php

/**
 * 后台父Action
 */
class BaseAction extends Action{

	protected $act_model = MODULE_NAME;

	protected $sel_model = MODULE_NAME;

	protected $act_db = null;

	protected $sel_db = null;

	protected $field = '*';

	protected $where = array();

	protected $order = 'id desc';

	protected $page_size = 15;

	protected $msg = '数据';

	function _initialize(){
		if(method_exists($this,'init')) $this->init();
		$login_user = session('login_user');
		if(!in_array(ACTION_NAME,explode(',','login')) && !$login_user){
			$this->redirect('Index/login');
		}
		$this->act_db = D($this->act_model);
		$this->sel_db = D($this->sel_model);
	}

	/**
	 * 模型列表
	 * @author Njs
	 */
	function index(){
		import("@.Util.Page");
		foreach($_GET as $key=>$val)
			$this->assign('get_'.$key,$val);
		if(method_exists($this,'setFilter')) $this->setFilter();
		$count = $this->sel_db->where($this->where)->count();
		$page = new Page($count,$this->page_size);
		$page->setConfig('theme'," %first% %upPage% <span>%linkPage% </span> %downPage% %end% ");
		$list = $this->sel_db->where($this->where)->order($this->order)->limit($page->firstRow.','.$page->listRows)->select();
		$this->assign('page',$page);
		$this->assign('list',$list);
		$this->display('index');
	}

	/**
	 * 添加/编辑
	 * @author Njs
	 */
	function data(){
		$sign = $this->act_db->where("id=".(int)$_GET['id'])->find();
		$this->assign('sign',$sign);
		$this->display('data');
	}

	/**
	 * 保存数据
	 * @author Njs
	 */
	public function save(){
		if(!IS_POST)
			$this->ajaxReturn('','非法提交数据',0);
		if(false!==$this->act_db->create()){
			$rst = intval($_POST['id'])>0 ? $this->act_db->where("id=".intval($_POST['id']))->save() : $this->act_db->add();
			if($rst!==false)
				$this->ajaxReturn('',$this->msg.'保存成功',1);
			else
				$this->ajaxReturn('',$this->msg.'保存失败'.$this->act_db->getError(),0);
		}else{
			$this->ajaxReturn('','验证未通过,错误信息:'.$this->act_db->getError(),0);
		}
	}

	/**
	 * 删除数据
	 * @author Njs
	 */
	public function del(){
		$id = !empty($_REQUEST['id']) ? $_REQUEST['id'] : 0;
		$rst = $this->act_db->where("id in($id)")->delete();
		if(false!==$rst)
			$this->ajaxReturn('',$this->msg.'删除成功',1);
		else
			$this->ajaxReturn('',$this->msg.'删除失败',0);
	}

}

?>